﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Sunny.Entity;
using Sunny.Model.RequestParam;
using Sunny.Service;
using Sunny.Util.Attributes;
using Sunny.Util.Enums;

namespace Sunny.Api.Controllers
{
    [ApiController]
    [Route("/system/dict/type")]
    public class SysDictTypeController
    {
        private readonly DictTypeService _dictTypeService;

        public SysDictTypeController(DictTypeService dictTypeService)
        {
            _dictTypeService = dictTypeService;
        }
        
        
        #region 字典类型列表
        [HasPermission("system:dict:list")]
        [HttpGet("list")]
        public async Task<Result> List([FromQuery]DictTypeListParam dictTypeListParam)
        {
            (List<SysDictType> dictTypes,int total) = await _dictTypeService.GetList(dictTypeListParam);
            return Result.Success(new
            {
                rows = dictTypes,
                total
            });
        }
        #endregion

        #region 字段详细信息
        [HasPermission("system:dict:query")]
        [HttpGet("{dictTypeId}")]
        public async Task<Result> Detail([FromRoute]long dictTypeId)
        {
            SysDictType sysDictType = await _dictTypeService.GetById(dictTypeId);
            
            return Result.Success(sysDictType);
        }


        #endregion
        
        #region 新增字典类型
        [HasPermission("system:dict:add")]
        [LogInfo(Title = "字典类型管理",BusinessType = BusinessType.Insert)]
        [HttpPost]
        public async Task<Result> Edit(SysDictType param)
        {
            await _dictTypeService.Add(param);
            return Result.Success();
        }

        #endregion

        #region 更新字典类型信息
        [HasPermission("system:dict:edit")]
        [LogInfo(Title = "字典类型管理",BusinessType = BusinessType.Update)]
        [HttpPut]
        public async Task<Result> Edit(DictTypeEditParam param)
        {
            await _dictTypeService.Update(param);
            return Result.Success();
        }

        #endregion
        
        #region 删除字段类型信息
        [HasPermission("system:dict:remove")]
        [LogInfo(Title = "字典类型管理",BusinessType = BusinessType.Delete)]
        [HttpDelete("{typeIds}")]
        public async Task<Result> Edit([FromRoute]string typeIds)
        {
            await _dictTypeService.Delete(typeIds);
            return Result.Success();
        }

        #endregion
    }
}